/*
 invite.go
 Created by hupfei on 2019-07-02
*/

package model

import (
	"database/sql"
	"fmt"
	"github.com/jmoiron/sqlx"
	"zimuzu_cvn_web_api/config"
	. "zimuzu_cvn_web_api/pkg/e"
)

type InviteInfo struct {
	Code       int64            `json:"code" db:"code"`
	Username   string           `json:"username" db:"nickname"`
	Cvnt       float64          `json:"cvnt" db:"cvnt"`
	InviteList []InviteUserInfo `json:"inviteList,omitempty" db:"inviteList,omitempty"`
}

type InviteUserInfo struct {
	Name       string  `json:"name" db:"phone_number"`
	Cvnt       float64 `json:"cvnt" db:"cvnt"`
	CreateTime string  `json:"createTime" db:"create_time"`
}

type AcceptInviteParams struct {
	Code      int64  `form:"code" binding:"required"`
	PhoneArea int    `form:"phoneArea" binding:"required"`
	Phone     string `form:"phone" binding:"required"`
	PhoneCode int    `form:"phoneCode" binding:"required"`
	Source    int    `form:"source"` //0 返佣邀请 1 矿工邀请
}

/*
 * @ Author: hupfei
 * @ Description: 检查用户是否存在cvn_invite_log中
 **/
func CheckUserExistInCvnInviteLog(uid int64) (bool, error) {
	var count int
	row := config.C.DB.DB.QueryRowx("SELECT count(*) FROM cvn_invite_log WHERE uid = ?", uid)
	err := row.Scan(&count)
	if err != nil {
		return false, err
	}
	if count == 0 {
		return false, nil
	}
	return true, nil
}

/*
 * @ Author: hupfei
 * @ Description: 查询受邀人列表
 **/
func GetInviteUserListFromDB(uid int64) (res []InviteUserInfo, err error) {
	sqlStr := fmt.Sprintf("SELECT phone_number, cvnt, FROM_UNIXTIME(create_time, '%%Y/%%m/%%d') AS create_time FROM cvn_invite_log WHERE inviter_id = %d", uid)
	err = sqlx.Select(config.C.DB.DB, &res, sqlStr)
	if err != nil {
		Log("err", "查询受邀人列表失败！", err.Error())
	}

	return
}

/*
 * @ Author: hupfei
 * @ Description: 插入受邀记录
 **/
func InsertInviteInfoToDB(inviterId int64, phoneArea int, phoneNumber string, uid int64) (err error) {
	sqlStr := fmt.Sprintf("INSERT INTO cvn_invite_log(inviter_id, phone_area, phone_number, create_time, uid) VALUES (%d, %d, %s, UNIX_TIMESTAMP(NOW()), %d)", inviterId, phoneArea, phoneNumber, uid)
	re := sqlx.MustExec(config.C.DB.DB, sqlStr)
	_, err = re.LastInsertId()
	if err != nil {
		Log("err", "插入受邀记录失败:", err.Error())
	}
	return
}

/*
 * @ Author: hupfei
 * @ Description: 通过返佣cvn_log_id查询返佣者uid
 **/
func SearchLogBackUid(relateId string, coinType int) (uid int64, err error) {
	sqlStr := "SELECT uid FROM "
	if coinType == 0 {
		sqlStr += "cvn_log "
	} else {
		sqlStr += "usdt_log "
	}
	sqlStr += fmt.Sprintf("WHERE id = %s", relateId)
	err = sqlx.Get(config.C.DB.DB, &uid, sqlStr)
	if err == sql.ErrNoRows {
		err = nil
	}
	if err != nil {
		Log("err", "查询uid失败！", err.Error())
	}
	return
}

//GetInviteUidByMoile  根据手机号查询邀请人uid（邀请码）
//Auth: Xu
func GetInviteUidByMoile(mobile string, area int) (inviterId string, err error) {
	sqlStr := fmt.Sprintf("SELECT inviter_id FROM cvn_invite_log WHERE phone_number  = '"+mobile+"' and phone_area = %d", area)
	err = sqlx.Get(config.C.DB.DB, &inviterId, sqlStr)
	if err == sql.ErrNoRows {
		err = nil
	}
	if err != nil {
		Log("err", "查询邀请人uid失败！", err.Error())
	}
	return
}

//UpdateUidByMoile 更新被邀请人的uid
//Auth: Xu
func UpdateUidByMoile(mobile string, area int, uid int64) (err error) {
	sqlStr := fmt.Sprintf("update cvn_invite_log set uid = %d WHERE phone_number  = '"+mobile+"' and phone_area = %d", uid, area)
	re := sqlx.MustExec(config.C.DB.DB, sqlStr)
	_, err = re.RowsAffected()
	if err != nil {
		Log("err", "更新被邀请人信息失败！", err.Error())
	}
	return
}
